package admin.controllers;

import helpers.CrawlerHelper;
import helpers.GoogleStorageHelper;
import helpers.IdUniqueHelper;
import helpers.StringHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import debugs.TemplateDebug;

import admin.forms.ApplicationTemplateForm;
import admin.models.ApplicationTemplateModel;
import entities.ApplicationTemplate;
import entities.Template;

@SuppressWarnings("serial")
public class ApplicationTemplateAddServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		List<Template> templates = CrawlerHelper.getAllTemplate();
		/*TemplateModel templateModel = TemplateModel.getInstance();
		List<Template> templates = templateModel.listAll();*/
		
		ApplicationTemplateModel appTemplateModel = ApplicationTemplateModel.getInstance();
		ApplicationTemplate appTemplate = new ApplicationTemplate();
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("template", appTemplate);
		request.setAttribute("templates", templates);
		try {
			request.getRequestDispatcher("/admin/pages/application_template_add_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		ApplicationTemplateModel appTemplateModel = ApplicationTemplateModel.getInstance();
		ApplicationTemplate appTemplate = new ApplicationTemplate();
		
		TreeMap<String, Object> form = ApplicationTemplateForm.checkValid(request, appTemplate);
		appTemplate = (ApplicationTemplate) form.get("template");
		check = (TreeMap<String, String>) form.get("checkValid");
		
		if (check.size() == 0) {
			
			appTemplate = getContentTemplate(appTemplate, appTemplate.getId());
			
			appTemplateModel.insert(appTemplate);
			
			request.setAttribute("success", "Creat new template " + appTemplate.getId() + " success");
			
			appTemplate = new ApplicationTemplate();
		}
		
		List<Template> templates = CrawlerHelper.getAllTemplate();
		/*TemplateModel templateModel = TemplateModel.getInstance();
		List<Template> templates = templateModel.listAll();*/
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("template", appTemplate);
		request.setAttribute("templates", templates);
		try {
			request.getRequestDispatcher("/admin/pages/application_template_add_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	private ApplicationTemplate getContentTemplate(ApplicationTemplate appTemplate, String templateId) {
		
		Template template = CrawlerHelper.getTemplate(templateId);
		/*TemplateModel templateModel = TemplateModel.getInstance();
		Template template = templateModel.get(templateId);*/
		
		try {
			/*
			 * Get list file by request to python application
			 */
			
			String[] splitFileName = GoogleStorageHelper.getListFile(templateId);
			/*
			 * read all files and compress
			 */
			for (String filename : splitFileName) {
				String content = "";
				
				filename = filename.replace("/gs/", "http://");
				if (filename.contains("home.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setHome(content);
				} 
				
				if (filename.contains("header.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setHeader(content);
				}
				
				if (filename.contains("footer.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setFooter(content);
				}
				
				if (filename.contains("layouts.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setLayout(content);
				}
				
				if (filename.contains("contact.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setContact(content);
				}
				
				if (filename.contains("static.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setStaticPage(content);
				}
				
				if (filename.contains("gsearch.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setSearch(content);
				}
				
				if (filename.contains("library.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setLibrary(content);
				}
			}
			
			appTemplate.setBaseUrl(template.getBaseUrl());
			
		} catch (Exception e) {
			e.printStackTrace(System.err);
			System.out.println(
							"Error when trying to download template. Please try again later !");
		}
		
		
		return appTemplate;
	}
}
